package com.k2.gulimall.member.controller;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import com.k2.gulimall.member.entity.MemberEntity;
import com.k2.gulimall.member.service.MemberService;
import com.k2.gulimall.member.vo.MemberDetailsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.k2.gulimall.member.entity.IntegrationChangeHistoryEntity;
import com.k2.gulimall.member.service.IntegrationChangeHistoryService;
import com.k2.common.utils.PageUtils;
import com.k2.common.utils.R;



/**
 * 积分变化历史记录
 *
 * @author changchaojie
 * @email changchaojie@gmail.com
 * @date 2020-11-09 17:36:10
 */
@RestController
@RequestMapping("member/integrationchangehistory")
public class IntegrationChangeHistoryController {
    @Autowired
    private IntegrationChangeHistoryService integrationChangeHistoryService;
    @Autowired
    private MemberService memberService;

    //当前时间
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
    String dateString = df.format(new Date());
    ParsePosition pos = new ParsePosition(8);
    Date data;
    {
        try {
            data = df.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = integrationChangeHistoryService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		IntegrationChangeHistoryEntity integrationChangeHistory = integrationChangeHistoryService.getById(id);

        return R.ok().put("integrationChangeHistory", integrationChangeHistory);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody IntegrationChangeHistoryEntity integrationChangeHistory){
		integrationChangeHistoryService.save(integrationChangeHistory);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody IntegrationChangeHistoryEntity integrationChangeHistory){
		integrationChangeHistoryService.updateById(integrationChangeHistory);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		integrationChangeHistoryService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }



    @RequestMapping("/changeMemberIntegration")
    public R changeMemberIntegration(@RequestBody MemberDetailsVo memberDetailsVo){
        Long memberId = memberDetailsVo.getId();
        Integer change = memberDetailsVo.getIntegration();
        //获取会员信息
        MemberEntity memberEntity = memberService.getById(memberId);
        //
        IntegrationChangeHistoryEntity integrationChangeHistory = new IntegrationChangeHistoryEntity();
        integrationChangeHistory.setMemberId(memberId);//会员id
        integrationChangeHistory.setCreateTime(data);//创建时间
        if(change!=null) {//管理员添加
            integrationChangeHistory.setChangeCount(change-memberEntity.getIntegration());
            memberEntity.setIntegration(change);
            integrationChangeHistory.setSourceTyoe(1);
        }else{
            integrationChangeHistory.setChangeCount(2);
            memberEntity.setGrowth(memberEntity.getGrowth()+2);
            integrationChangeHistory.setSourceTyoe(0);
        }
        //添加成长值记录，保存更改后的成长值
        if (integrationChangeHistoryService.save(integrationChangeHistory)&&memberService.saveOrUpdate(memberEntity)){
            return R.ok().put("code",0);
        }else{
            return R.error(1,"请求参数错误");
        }
    }

}
